--------------------------------------------------------------------------------
-- 数据库名称       : RYTreasureDB
-- 数据对象名称     : dbo.GSP_GR_WriteUserRoomCard
-- DecryptSQL 版本  : Ver 3.2.0
-- DecryptSQL 网站  : http://www.devlib.net/cn/
--------------------------------------------------------------------------------

ALTER PROC [dbo].[CPP_User_RoomCard_Write]
  @dwUserID INT,
  @wCostScore INT,
  @wCostType INT,
  @dwKindID INT
AS

SET NOCOUNT ON

BEGIN
  DECLARE @ID INT = 0
  DECLARE @RoomCard INT = 0 -- 当前房卡数
  SELECT @ID = ID, @RoomCard = RoomCard FROM UserRoomCard WHERE UserID = @dwUserID AND KindID = @dwKindID
  IF @@ROWCOUNT=0 BEGIN
    INSERT INTO UserRoomCard ([UserID], [KindID]) VALUES (@dwUserID, @dwKindID)
    SET @ID = @@IDENTITY
  END

  DECLARE @lRoomCard INT = @RoomCard + @wCostScore
  
  IF @wCostScore < 0 BEGIN
    IF @lRoomCard < 0 BEGIN
      RETURN 2  -- 房卡数不够扣除的情况
    END
  END

  UPDATE UserRoomCard SET RoomCard = @lRoomCard WHERE ID = @ID

  DECLARE @str NVARCHAR(80)
  IF @wCostType=1 BEGIN
    SET @str='房主付款消耗房卡:' + CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=2 BEGIN
    SET @str='AA付款消耗房卡:'  + CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=3 BEGIN
    SET @str='大赢家付款消耗房卡:' + CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=7 BEGIN
    SET @str='完成分享奖励房卡:'+ CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=8 BEGIN
    SET @str='完成实名认证奖励房卡:'+ CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=9 BEGIN
    SET @str='完成绑定邀请码奖励房卡:'+ CAST(@wCostScore AS NVARCHAR) + ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=17 BEGIN
    SET @str='丢一个炸弹消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=18 BEGIN
    SET @str='丢一只拖鞋消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=19 BEGIN
    SET @str='丢一只肥鸡消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=20 BEGIN
    SET @str='送一朵玫瑰消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=21 BEGIN
    SET @str='丢一个番茄消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE IF @wCostType=22 BEGIN
    SET @str='丢一个炸弹消耗房卡:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END ELSE BEGIN
    SET @str='一个未知类型的房卡消耗:' + CAST(@wCostScore AS NVARCHAR)+ ' 剩余房卡数量:' + CAST(@lRoomCard AS NVARCHAR)
  END

  INSERT INTO RYRecordDB.dbo.CostRoomCardRecord(
    dwUserID, ServerID,KindID, dwCost, dwCostType, [describe], beleftRoomCard
  ) VALUES (
    @dwUserID, @dwServerID,@dwKindID, @wCostScore, @wCostType, @str,@lRoomCard
  )
  
  RETURN 0
  
END
